/*
 * 
Copyright (c) 2010 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the above
copyright notice and the following two paragraphs appear in all copies
of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS..
 */

package org.andersonlab.trailsdm.control;

import java.io.Serializable;
import org.andersonlab.trailsdm.control.DataConduit.ChangeCallBack;


/**
 * @author John Christopher Anderson
 */


public class Datum implements Serializable {
    // <editor-fold defaultstate="collapsed" desc="CLASS VARIABLES">
    private final String uuid = createUUID();
    private String name;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="CONSTRUCTORS">
    /**
     * Constructor for a new Datum, not retrieval of one
     * @param name 
     */
    public Datum(String name) {
        this.name = name;
    }
    
    /**
     * Constructor needed for GWT
     */
    public Datum() {
        
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="CLASS METHODS">
    public void save() {
        getConduit().save(this, new ChangeCallBack() {
            @Override
            public void wasSuccessful(boolean wasit) {
            }
        });
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="GETTERS">
    public String getUUID() {
        return uuid;
    }
    
    public DataConduit getConduit() {
        return conduit;
    }
    
    public DisplayController getDisplayer() {
        return displayer;
    }
    
    public String getName() {
        return name;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SETTERS">
    public void setName(String newname) {
        name = newname;
        save();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="INNER CLASSES">

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="STATIC VARIABLES">
    private transient static DataConduit conduit = null;
    private transient static DisplayController displayer = null;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="STATIC METHODS">
    private static String createUUID() {
        StringBuilder out = new StringBuilder();
        for(int i=0; i<30; i++) {
            double dval = Math.floor(Math.random()*26 + 65);
            int ival = (int) dval;
            char cval = (char) ival;
            out.append(cval);
        }
        return out.toString();
    }
    
    public static void setDisplayer(DisplayController cont) {
        if(displayer!=null) {
            return;
        }
        displayer = cont;
               
    }
    
    public static void setConduit(DataConduit dconduit) {
        if(conduit!=null) {
            return;
        }
        conduit = dconduit;
               
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="STATIC">

    // </editor-fold>

}
